﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Extension;
using UnityEditor.UI;
using System.IO;
using System.Extension;
using System.Reflection.Extension;

[CustomEditor(typeof(SubjectiveScrollList))]
public class SubjectiveScrollListInspector : ScrollRectEditor
{
    SerializedProperty subject, templateCell, m_Content;
    RectTransform cellObject;
    bool editTemplate, preEditTemplate;

    protected override void OnEnable()
    {
        base.OnEnable();
        subject = serializedObject.FindProperty("m_subject");
        templateCell = serializedObject.FindProperty("TemplateCell");
       
        m_Content = serializedObject.FindProperty("m_Content");
        RectTransform contentTransform = m_Content.objectReferenceValue as RectTransform;

        if (contentTransform)
        {
            cellObject = contentTransform.childCount > 0 ? contentTransform.GetChild(0) as RectTransform : null;
            editTemplate = cellObject.isNotNull();
        }
        preEditTemplate = editTemplate;
    }


    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(subject);
        if (subject.FindPropertyRelative("content").objectReferenceValue)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PropertyField(templateCell);
            if (m_Content.objectReferenceValue && templateCell.objectReferenceValue)
            {
                editTemplate = GUILayout.Toggle(editTemplate, "E", EditorStyles.miniButton, GUILayout.Width(18));

                if (GUI.changed && preEditTemplate != editTemplate)
                {
                    if (editTemplate)
                        InstanciateTemplateCell();
                    else
                        ApplyTemplateCell();
                    preEditTemplate = editTemplate;
                }
            }

            EditorGUILayout.EndHorizontal();
        }
        serializedObject.ApplyModifiedProperties();
        base.OnInspectorGUI();
    }

    void InstanciateTemplateCell()
    {
        GameObject templatePrefab = templateCell.objectReferenceValue as GameObject;
        cellObject = (GameObject.Instantiate(templatePrefab) as GameObject).transform as RectTransform;
        RectTransform contentTransform = m_Content.objectReferenceValue as RectTransform;
        cellObject.transform.SetParent(contentTransform);
        cellObject.transform.localScale = Vector3.one;
    }

    void ApplyTemplateCell()
    {
        //GameObject templatePrefab = templateCell.objectReferenceValue as GameObject;
        cellObject.SetParent(null);
        PrefabUtility.ReplacePrefab(cellObject.gameObject, templateCell.objectReferenceValue, ReplacePrefabOptions.ReplaceNameBased);
        GameObject.DestroyImmediate(cellObject.gameObject);
    }

    [MenuItem("GameObject/UI/Subjective/ScrollList")]
    static void CreateSubjectiveText()
    {
        List<GameObject> newObjects = GameObjectMenu.CreateNewControlInHierarchy("ScrollList", typeof(SubjectiveScrollList));

        newObjects.ForEach(o => {
            SubjectiveScrollList slist = o.GetComponent<SubjectiveScrollList>();
            slist.content = GameObjectMenu.CreateNewObject(o, "Content", typeof(ContentSizeFitter)).transform as RectTransform; 
        });
    }
}
